<?php
// Test of R0910CMS
// created by TLightSky
// time : 20100404

class R_Action_Manage extends R_MVCManageAbstract {
	public  $enableNextAction = false;
	
	private $defaultClass;
	private $defaultMethod;
	private $class;
	private $method;

	// check & set
	public function nextAction($class=null, $method=null){
		if(!$this->enableNextAction) {
			throw new R_Exception("Action : next action not enabled!");
		}
		// no param to judge is there a next action
		if(null==$class) {
			return $this->checkDefaultActionSession();
		} else {
			return $this->setNextActionSession($class,$method);
		}
	}
	// load
	public function loadNextAction() {
		return $this->loadNextActionSession();
	}
	public function destroyNextAction() {
		return $this->destroyNextActionSession();
	}

	// with session
	private function checkDefaultActionSession() {
			if( isset( $_SESSION["r0910ms"]["class"] ) && isset( $_SESSION["r0910ms"]["method"] ) ){
				return true;
			} else  {
				return false;
			}
	}
	private function setNextActionSession($class=null, $method=null) {
		$_SESSION["r0910ms"]["nextClass"] =$class;
		$_SESSION["r0910ms"]["nextMethod"]=$method;
		return $this;
	}
	private function loadNextActionSession($class=null, $method=null) {
		return $this->setAction($_SESSION["r0910ms"]["nextClass"],$_SESSION["r0910ms"]["nextMethod"]);
	}
	public function destroyNextActionSession(){
		if($this->enableNextAction) {
	    	unset( $_SESSION["r0910ms"]["nextClass"] );
			unset( $_SESSION["r0910ms"]["nextMethod"] );
		}
			return $this;
	}
    
	public function defaultAction($class=null, $method=null){
		// no param to judge is there a next action
		if(null==$class) {
			return isset($this->defaultClass);
		} else {
			return $this->setDefaultAction($class,$method);
		}
	}
	public function loadDefaultAction() {
		return $this->setAction($this->defaultClass,$this->method= $this->defaultMethod);
	}
	public function setDefaultAction($class=null,$method=null) {
		$this->defaultClass =$class;
		$this->defaultMethod=$method;
		return $this;
	}
	
    public function parsePostAction(){
		if(isset($_GET['a'])) {
			list($this->class, $this->method) = explode('/', $_GET['a']);
			return true;
		} else {
			return false;
		}
	}
	public function parseNextAction() {
		if(!$this->enableNextAction) {
			return false;
		}
		if($this->nextAction()) {
			$this->loadNextAction();
			return true;
		} else {
			return false;
		}
	}
	public function parseDefaultAction() {
		if($this->defaultAction()) {
			$this->loadDefaultAction();
			return true;	
		} else {
			return false;
		}
	}
	
	private function setAction($class=null,$method=null) {
		$this->class		=	$class;
		$this->method		=	$method;
		return $this;
	}
    
	// @Override
    public function dispatch($class=null, $method=null) {
    	if( $class != null ){
    		$this->setAction($class,$method);
    	} else if($this->parsePostAction()) {
    	} else if($this->parseNextAction()) {
    	} else if($this->parseDefaultAction()){
    	} else {
    		throw new R_Exception("There is no fitted Action!");
    	}
    	$this->destroyNextAction();
    	$class = $this->class;
    	$method= $this->method;
		if($class==__CLASS__) {
			parent::dispatch($class,$method);
		}
		$this->getClass($class);
	    if(method_exists(getInstance($class),$method)) {
      	  return getInstance($class)->dispatch($class,$method);
	    } else {
		  throw new R_Exception_ParamWrong("Class $class does not have method $method!");
	    }
    }
}
